package com.widget

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.CountDownTimer
import android.view.View
import com.aop.annotation.SingleClick
import com.helper.utils.IsEmpty


import com.sh.doctors.cloud.R
import com.sh.doctors.cloud.databinding.DialogCommonBinding

/**
 * 界面描述：
 * <p>
 * Created by tianyang on 2018/6/6.
 */
class CommonDialog(context: Context?) : BaseDialog<DialogCommonBinding>(context) {
    var content = ""
    var subContent = ""
    var cancelText = "取消"
    var confirmText = "确认"
    var showCancelBtn = true
    var showConfirmBtn = true
    var confirmColor = R.color.C07C160
    var cancelColor = R.color.black
    var showTimer = false
    var timeType = 1
    var time = 0

    var onCancelClickListener: OnCancelClickListener? = null
    var onConfirmClickListener: OnConfirmClickListener? = null

    private lateinit var mTimer: CountDownTimer

    override fun getLayoutId(): Int = R.layout.dialog_common

    override fun initView() {
        val params = window!!.attributes
        val width = (mContext as Activity).resources.getDimension(R.dimen.size220dp)
        params.width = width.toInt()
        window!!.attributes = params
        setCancelable(false)

        if (showTimer) {
            mTimer = object : CountDownTimer((time * 1000).toLong(), 1000) {
                // 第一个参数是总的倒计时时间
                // 第二个参数是每隔多少时间(ms)调用一次onTick()方法
                override fun onTick(millisUntilFinished: Long) {
                    if (timeType == 1) {
                        mBinding.confirmTv.text = "${confirmText}(${millisUntilFinished / 1000}s)"
                    } else {
                        mBinding.cancelTv.text = "${cancelText}(${millisUntilFinished / 1000}s)"
                    }
                }

                override fun onFinish() {
                    dismiss()
                    if (timeType == 1) {
                        onConfirmClickListener?.onConfirmClick(this@CommonDialog)
                    } else {
                        onCancelClickListener?.onCancelClick(this@CommonDialog)
                    }
                }
            }
        }

        if (!showCancelBtn) {
            mBinding.cancelTv.visibility = View.GONE
            mBinding.dividerView.visibility = View.GONE
//            mBinding.confirmTv.background = mContext.resources.getDrawable(R.drawable.dialog_bottom)
        }
        if (!showConfirmBtn) {
            mBinding.confirmTv.visibility = View.GONE
            mBinding.dividerView.visibility = View.GONE
//            mBinding.cancelTv.background = mContext.resources.getDrawable(R.drawable.dialog_bottom)
        }
    }

    override fun show() {
        super.show()
        if (showTimer) {
            mTimer.cancel()
            mTimer.start()
        }

        mBinding.cancelTv.text = cancelText
        mBinding.cancelTv.setTextColor(mContext.resources.getColor(cancelColor))
        mBinding.confirmTv.text = confirmText
        mBinding.confirmTv.setTextColor(mContext.resources.getColor(confirmColor))
        mBinding.contentTv.text = content

        if (!IsEmpty.check(subContent)) {
            mBinding.subContentTv.visibility = View.VISIBLE
            mBinding.subContentTv.text = subContent
        } else {
            mBinding.subContentTv.visibility = View.GONE
        }

    }

    override fun dismiss() {
        if (showTimer) {
            mTimer.cancel()
        }
        super.dismiss()
    }

    override fun loadData() {

    }

    @SingleClick
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.cancel_tv -> {
                dismiss()
                onCancelClickListener?.onCancelClick(this)
            }
            R.id.confirm_tv -> {
                dismiss()
                onConfirmClickListener?.onConfirmClick(this)
            }
        }
    }

    fun setOnCancelClickListener(listener: OnCancelClickListener): CommonDialog {
        onCancelClickListener = listener
        return this
    }

    fun setOnConfirmClickListener(listener: OnConfirmClickListener): CommonDialog {
        onConfirmClickListener = listener
        return this
    }

    interface OnCancelClickListener {
        fun onCancelClick(dialog: Dialog)
    }

    interface OnConfirmClickListener {
        fun onConfirmClick(dialog: Dialog)
    }
}